package kr.hkit.summary_java.erp.UI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.GridLayout;

import javax.swing.JButton;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.erp.dao.DaoInterface;
import kr.hkit.summary_java.erp.dto.Department;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.SQLException;

public class PanelDept extends JPanel implements ActionListener {
	private JButton btnOk;
	private InputPanel panel_deptno;
	private InputPanel panel_deptname;
	private InputPanel panel_floor;
	private Department d;

	private DaoInterface<Department> dao;

	/**
	 * Create the panel.
	 */
	public PanelDept(DaoInterface<Department> dao) {
		this.dao = dao;
		initialize();
	}

	private void initialize() {
		setLayout(new GridLayout(0, 1, 0, 0));

		panel_deptno = new InputPanel("부서번호");
		add(panel_deptno);
		panel_deptno.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				int no = Integer
						.parseInt(panel_deptno.getTf().getText().trim());
				d = new Department();
				d.setDeptNo(no);
				try {
					d = dao.searchItem(d);
					panel_deptname.setTf(d.getDeptName());
					panel_floor.setTf(d.getFloor() + "");
					if (!btnOk.getText().equals("검색")) {
						panelEnable();
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (NullPointerException e1) {
					if (!btnOk.getText().equals("추가"))
						JOptionPane.showMessageDialog(PanelDept.this,
								"해당 부서가 없습니다.");
					else
						JOptionPane.showMessageDialog(PanelDept.this, "추가 가능");
				}
			}

		});

		panel_deptname = new InputPanel("부서명");
		add(panel_deptname);

		panel_floor = new InputPanel("위치");
		add(panel_floor);

		JPanel panel_btn = new JPanel();
		add(panel_btn);

		btnOk = new JButton();
		btnOk.addActionListener(this);
		panel_btn.add(btnOk);
	}

	private Department getDept() {
		int dno = Integer.parseInt(panel_deptno.getTf().getText().trim());
		String name = panel_deptname.getTf().getText();
		int floor = Integer.parseInt(panel_floor.getTf().getText());
		return new Department(dno, name, floor);
	}

	private void panelDisable() {
		panel_deptno.getTf().setEnabled(true);
		panel_deptname.getTf().setEnabled(false);
		panel_floor.getTf().setEnabled(false);
	}

	private void panelEnable() {
		panel_deptno.getTf().setEnabled(false);
		panel_deptname.getTf().setEnabled(true);
		panel_floor.getTf().setEnabled(true);
	}

	private void panelClear() {
		panel_deptno.setTf("");
		panel_deptname.setTf("");
		panel_floor.setTf("");
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가")) {
			actionPerformedBtnAdd(e);
		} else if (e.getActionCommand().equals("삭제")) {
			actionPerformedBtnDel(e);
		} else if (e.getActionCommand().equals("수정")) {
			actionPerformedBtnUpdate(e);
		} else if (e.getActionCommand().equals("검색")) {
			actionPerformedBtnSearch(e);
		} else {// 리스트보기
			actionPerformedBtnList(e);
		}
	}

	private void actionPerformedBtnList(ActionEvent e) {
	}

	private void actionPerformedBtnSearch(ActionEvent e) {
		int no = Integer.parseInt(panel_deptno.getTf().getText().trim());
//		d = new Department(); 어차피 init에서 생성하고 오니까 생략
		d.setDeptNo(no);
		try {
			d = dao.searchItem(d);
			panel_deptname.setTf(d.getDeptName());
			panel_floor.setTf(d.getFloor() + "");
			panelEnable();
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (NullPointerException e1) {
			JOptionPane.showMessageDialog(this, "존재하지 않음");
			panel_deptno.setTf("");
		}
	}

	private void actionPerformedBtnUpdate(ActionEvent e) {
		d = getDept();
		try {
			dao.updateItembyNo(d);
			JOptionPane.showMessageDialog(this, "수정되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "수정실패 다시 확인하세요.");
		}
	}

	private void actionPerformedBtnDel(ActionEvent e) {
		d = getDept();
		try {
			dao.deleteItembyNo(d);
			JOptionPane.showMessageDialog(this, "삭제되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}

	private void actionPerformedBtnAdd(ActionEvent e) {
		d = getDept();
		try {
			dao.insertItem(d);
			JOptionPane.showMessageDialog(this, "입력되었습니다");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
		}
	}
	
	public void setBtnOk(String btnOk) {
		this.btnOk.setText(btnOk);
	}

}
